package com.dl7.player.utils; /**
 * Copyright 2014 Zhenguo Jin (jinzhenguo1990@gmail.com)
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import ohos.aafwk.ability.Ability;
import ohos.agp.animation.AnimatorValue;
import ohos.agp.window.service.*;
import ohos.app.Context;
import ohos.bundle.AbilityInfo;
import ohos.global.configuration.Configuration;
import java.util.Optional;

/**
 * 窗口工具箱
 *
 * @author zhenguo
 */
public final class WindowUtils {

    private static final int ROTATION_0 = 0;
    private static final int ROTATION_90 =1;
    private static final int ROTATION_180 = 2;
    private static final int ROTATION_270 = 3;

    /**
     * Don't let anyone instantiate this class.
     */
    private WindowUtils() {
        throw new Error("Do not need instantiate!");
    }

    /**
     * 获取当前窗口的旋转角度
     *
     * @param  ability
     * @return int
     */
    public static int getDisplayRotation(Ability ability) {
        DisplayManager instance = DisplayManager.getInstance();
        Optional<Display> defaultDisplay = instance.getDefaultDisplay(ability);
        int rotation = defaultDisplay.get().getRotation();
        switch (rotation) {
            case ROTATION_0:
                return 0;
            case ROTATION_90:
                return 90;
            case ROTATION_180:
                return 180;
            case ROTATION_270:
                return 270;
            default:
                return 0;
        }
    }

    /**
     * 当前是否是横屏
     *
     * @param context context
     * @return boolean
     */
    public static final boolean isLandscape(Context context) {
        Configuration configuration = context.getResourceManager().getConfiguration();
        return configuration.direction == Configuration.DIRECTION_HORIZONTAL;
    }

    /**
     * 当前是否是竖屏
     *
     * @param context context
     * @return boolean
     */
    public static final boolean isPortrait(Context context) {
        Configuration configuration = context.getResourceManager().getConfiguration();
        return configuration.direction == Configuration.DIRECTION_VERTICAL;
    }

    /**
     * 调整窗口的透明度  1.0f,0.5f 变暗
     *
     * @param from    from>=0&&from<=1.0f
     * @param to      to>=0&&to<=1.0f
     * @param context 当前的activity
     */
    public static void dimBackground(final float from, final float to, Ability context) {
        final Window window = context.getWindow();
        MyValueAnimator animatorValue = MyValueAnimator.ofFloat(from, to);
        animatorValue.setDuration(500);
        animatorValue.setValueUpdateListener(new AnimatorValue.ValueUpdateListener() {
            @Override
            public void onUpdate(AnimatorValue animatorValue, float v) {
                WindowManager.LayoutConfig layoutConfig = window.getLayoutConfig().get();
                layoutConfig.alpha = v;
                window.setLayoutConfig(layoutConfig);
            }
        });
        animatorValue.start();
    }

    /**
     * 获取界面方向
     */
    public static AbilityInfo.DisplayOrientation getScreenOrientation(Ability ability) {
        Optional<Display> defaultDisplay = DisplayManager.getInstance().getDefaultDisplay(ability);
        int rotation = defaultDisplay.get().getRotation();

        int width = defaultDisplay.get().getRealAttributes().width;
        int height = defaultDisplay.get().getRealAttributes().height;
        AbilityInfo.DisplayOrientation orientation;
        // if the device's natural orientation is portrait:
        if ((rotation == ROTATION_0 || rotation == ROTATION_180) && height > width
                || (rotation == ROTATION_90 || rotation == ROTATION_270) && width > height) {
            switch (rotation) {
                case ROTATION_0:
                case ROTATION_180:
                    orientation = AbilityInfo.DisplayOrientation.PORTRAIT;
                    break;
                case ROTATION_90:
                case ROTATION_270:
                    orientation = AbilityInfo.DisplayOrientation.LANDSCAPE;
                    break;
                default:
                    orientation = AbilityInfo.DisplayOrientation.PORTRAIT;
                    break;
            }
        }
        // if the device's natural orientation is landscape or if the device
        // is square:
        else {
            switch (rotation) {
                case ROTATION_0:
                case ROTATION_180:
                    orientation = AbilityInfo.DisplayOrientation.LANDSCAPE;
                    break;
                case ROTATION_90:
                case ROTATION_270:
                    orientation = AbilityInfo.DisplayOrientation.PORTRAIT;
                    break;
                default:
                    orientation = AbilityInfo.DisplayOrientation.LANDSCAPE;
                    break;
            }
        }

        return orientation;
    }
}